#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time   : 2022/3/28 15:21
# @Author : 闫峰
"""
日志装饰器，控制程序日志输入，默认为 True
如设置 False，则程序不会打印日志
"""
from functools import wraps

from common.setting import ensure_path_sep
from utils.mysqlUtils.db_pool import Database


def log_decorator(switch: bool):
    """
    封装日志装饰器, 打印请求信息
    :param switch: 定义日志开关
    :return:
    """
    def decorator(func):
        @wraps(func)
        def swapper(*args, **kwargs):

            # 判断日志为开启状态，才打印日志
            res = func(*args, **kwargs)
            # 判断日志开关为开启状态
            if switch:
                _log_msg = f"\n======================================================\n" \
                               f"用例标题: {res.detail}\n" \
                               f"请求路径: {res.url}\n" \
                               f"请求方式: {res.method}\n" \
                               f"请求头:   {res.headers}\n" \
                               f"请求内容: {res.request_body}\n" \
                               f"接口响应内容: {res.response_data}\n" \
                               f"接口响应时长: {res.res_time} ms\n" \
                               f"Http状态码: {res.status_code}\n" \
                               "====================================================="
                _is_run = res.is_run
                # 判断正常打印的日志，控制台输出绿色
                if _is_run in (True, None) and res.status_code == 200:
                    print(_log_msg)
                else:
                    # 失败的用例，控制台打印红色
                    print(_log_msg)
            return res
        return swapper
    return decorator

def log_print_decorator(switch, filter_mame):

    def decorator(func):
        @wraps(func)
        def swapper(*args, **kwargs):
            data_set = kwargs.get('data_set')
            if not isinstance(data_set, set):
                data_set = set(data_set)
            if not data_set:
                return set()
            # 判断日志为开启状态，才打印日志
            filter_data_set = func(*args, **kwargs)
            # 判断日志开关为开启状态
            if switch:
                print(f"{filter_mame}条件 过滤前数据集: {data_set-filter_data_set}")
            return filter_data_set
        return swapper
    return decorator


def log_print_decorator_allegro_publish(switch, filter_mame,source_id=1,strategy_id=1):



    def decorator(func):
        @wraps(func)
        def swapper(*args, **kwargs):
            data_set = kwargs.get('data_set')
            if not isinstance(data_set, set):
                data_set = set(data_set)
            # 判断日志为开启状态，才打印日志
            filter_data_set = func(*args, **kwargs)
            # 判断日志开关为开启状态
            if switch:
                print(f"{filter_mame}条件 过滤了数据集: {data_set-filter_data_set}")
            listing_list = list(map(lambda sku:(source_id,sku,strategy_id,filter_mame),data_set-filter_data_set))
            publish_list = list(map(lambda sku:(source_id,sku,strategy_id),filter_data_set))
            db_java_test = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "test_jdw_database")
            insert_sql = """
            INSERT INTO monitor.t_allegro_publish_result 
            (source_id,sku,strategy_id,filter_rules)
            VALUES (%s,%s,%s,%s);
            """
            publish_list_sql = """
            INSERT INTO monitor.t_allegro_publish_result_ 
            (source_id,sku,strategy_id)
            VALUES (%s,%s,%s);
            """
            db_java_test.execute_many(insert_sql, list(listing_list))
            db_java_test.execute_many(publish_list_sql, publish_list)
            return filter_data_set
        return swapper
    return decorator



def log_print_decorator_temu_publish(switch, filter_mame):



    def decorator(func):
        @wraps(func)
        def swapper(*args, **kwargs):
            sku_list = kwargs.get('sku_list')
            if not isinstance(sku_list, set):
                sku_list = set(sku_list)
            # 判断日志为开启状态，才打印日志
            filter_data_set = func(*args, **kwargs)
            # 判断日志开关为开启状态
            if switch:
                print(f"{filter_mame}条件 过滤了数据集: {sku_list-set(filter_data_set)}")
            # listing_list = list(map(lambda sku:(source_id,sku,strategy_id,filter_mame),sku_list-filter_data_set))
            # publish_list = list(map(lambda sku:(source_id,sku,strategy_id),filter_data_set))
            # db_java_test = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "test_jdw_database")
            # insert_sql = """
            # INSERT INTO monitor.t_allegro_publish_result
            # (source_id,sku,strategy_id,filter_rules)
            # VALUES (%s,%s,%s,%s);
            # """
            # publish_list_sql = """
            # INSERT INTO monitor.t_allegro_publish_result_
            # (source_id,sku,strategy_id)
            # VALUES (%s,%s,%s);
            # """
            # db_java_test.execute_many(insert_sql, list(listing_list))
            # db_java_test.execute_many(publish_list_sql, publish_list)
            return filter_data_set
        return swapper
    return decorator